package com.ewanghuiju.app.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.App;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.component.RxBus;
import com.ewanghuiju.app.model.event.SendEvent;
import com.ewanghuiju.app.pay.PayReslut;
import com.ewanghuiju.app.util.LogUtil;
import com.google.gson.Gson;
import com.muddzdev.styleabletoast.StyleableToast;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        api = WXAPIFactory.createWXAPI(this, Constants.WX_APPID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }


    @Override
    public void onResp(BaseResp resp) {
        //0	成功  -1	错误  -2	用户取消
//        LogUtil.e("onPayFinish, errCode = " + resp.errCode + ",resp:" + resp.errStr + new Gson().toJson(resp));
//        StyleableToast.makeText(App.getInstance(), "支付成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            RxBus.getDefault().post(new SendEvent("", Constants.TYPE_PAY_STATUS_RETURN, new PayReslut(PayReslut.WXPAY, resp.errCode)));
        }
        finish();
    }
}